<template>
  <div>
    <!--    搜索表单-->
    <div>
      <el-input style="width: 240px" placeholder="请输入图书名" v-model="params.bookName"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入借阅人" v-model="params.userName"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入书号" v-model="params.bookIsbn"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load()">
        <i class="el-icon-search"></i> 搜索
      </el-button>
      <el-button style="margin-left: 5px" type="danger" @click="reset">
        <i class="el-icon-refresh"></i>重置
      </el-button>
    </div>
    <!--增删改查-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增借阅 <i class="el-icon-edit-outline"></i></el-button>
    </div>
    <!--表单内容-->
    <el-table :data="tableData" stripe row-key="id" default-expand-all>
      <el-table-column fixed
                       prop="id"
                       label="编号"
                       width="60">
      </el-table-column>
      <el-table-column prop="bookName" label="书名"></el-table-column>
      <el-table-column prop="cover" label="封面" width="100">
        <template v-slot="scope">
          <el-image :src="scope.row.cover" :preview-src-list="[scope.row.cover]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="bookIsbn" label="书号"></el-table-column>
      <el-table-column prop="userId" label="用户ID" width="100"></el-table-column>
      <el-table-column prop="userName" label="用户名称" width="120"></el-table-column>
      <el-table-column prop="userPhone" label="用户联系方式"></el-table-column>
      <el-table-column prop="score" label="所需积分"></el-table-column>
      <el-table-column prop="createTime" label="借出时间"></el-table-column>
      <el-table-column prop="status" label="借出状态"></el-table-column>
      <el-table-column prop="days" label="借出天数"></el-table-column>
      <el-table-column prop="returnTime" label="还书时间"></el-table-column>
      <el-table-column prop="note" label="提醒">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.note === '正常'">{{ scope.row.note}}</el-tag>
          <el-tag type="primary" v-if="scope.row.note === '即将到期'">{{ scope.row.note}}</el-tag>
          <el-tag type="warning" v-if="scope.row.note === '已经到期'">{{ scope.row.note}}</el-tag>
          <el-tag type="warning" v-if="scope.row.note === '已过期'">{{ scope.row.note}}</el-tag>
        </template>
      </el-table-column>

      <!--操作-->
      <el-table-column
          fixed="right"
          label="操作"
          width="200">

        <template v-slot="scope">
          <el-button @click="returnBook(scope.row)" type="success" size="small" v-if="scope.row.status === '借出'">还书<i class="el-icon-edit"></i>
          </el-button>
          <el-popconfirm
              class="ml-5"
              title="您确定删除这行信息吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" type="danger" size="small">删除 <i class="el-icon-remove-outline"></i>
            </el-button>
          </el-popconfirm>

        </template>
      </el-table-column>


    </el-table>

    <!--    分页-->
    <div style="text-align: center; ">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

    <!--    新增表单-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" style="" :model="form" :rules="rules" ref="rulerForm">

        <el-form-item label="书号" prop="bookIsbn" >
          <el-select v-model="form.bookIsbn" filterable clearable  placeholder="请选择" @change="selectBook" style="width: 100%">
            <el-option
                v-for="item in books"
                :key="item.id"
                :label="item.isbn"
                :value="item.isbn">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" auto-complete="off" disabled placeholder="请输入书名" ></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-input v-model="form.cover" auto-complete="off" disabled placeholder="请输入封面"></el-input>
        </el-form-item>
        <el-form-item label="所需积分" prop="score">
          <el-input v-model="form.score" auto-complete="off" disabled placeholder="请输入积分" style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item label="借阅时长" prop="days">
          <el-input-number v-model="form.days" auto-complete="off" :min="1" :max="10" style="width: 100%;"  ></el-input-number>
        </el-form-item>
        <el-form-item label="借阅人" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择" filterable  clearable style="width: 100%" @change="selectUser">
            <el-option
                v-for="item in users"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="userName">
          <el-input v-model="form.userName" auto-complete="off" disabled  style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item label="借阅卡号" prop="userCardID">
          <el-input v-model="form.userCardID" auto-complete="off" disabled ></el-input>
        </el-form-item>
        <el-form-item label="借阅电话" prop="userPhone">
          <el-input v-model="form.userPhone" auto-complete="off" disabled ></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


    <!--    //修改-->
    <el-dialog title="用户信息" :visible.sync="updateTab" width="30%">
      <el-form label-width="80px" style="" :model="updateForm" :rules="rules" ref="rulerForm">

        <el-form-item label="书号" prop="bookIsbn" >
          <el-input v-model="updateForm.bookIsbn" auto-complete="off" disabled ></el-input>
        </el-form-item>
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="updateForm.bookName" auto-complete="off" disabled ></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-input v-model="updateForm.cover" auto-complete="off" disabled ></el-input>
        </el-form-item>
        <el-form-item label="所需积分" prop="score">
          <el-input v-model="updateForm.score" auto-complete="off" disabled  style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item label="借阅人" prop="userId">
          <el-input v-model="updateForm.userId" auto-complete="off" disabled></el-input>

        </el-form-item>
        <el-form-item label="借阅卡号" prop="userCardID">
          <el-input v-model="updateForm.userCardID" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="借阅电话" prop="userPhone">
          <el-input v-model="updateForm.userPhone" auto-complete="off" disabled></el-input>
        </el-form-item>


        <el-form-item label="天数" prop="days" style="display: none">
          <el-input v-model="updateForm.days" auto-complete="off" disabled placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="returnTime" prop="returnTime" style="display: none">
          <el-input v-model="updateForm.returnTime" auto-complete="off" disabled  placeholder="请输入电话"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelUpdate">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: "Borrow.vue",

  data() {


    return {
      tableData: [],
      books: [],
      users: [],
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10,
      },

      dialogFormVisible: false,
      updateTab: false,
      form: {},
      updateForm: {},

      rules: {
        name: [
          {required: true, message: '请输入类名', trigger: 'blur'},
        ],
      },

    }
  },

  created() {
    this.load();
    request.get('/book/list').then(res => {
      this.books = res.data
    })
    request.get('/user/list').then(res => {
      this.users = res.data
    })

  },


  methods: {
    handleChange() {
    },



    selectBook() {
      const book = this.books.find(v => v.isbn === this.form.bookIsbn)
      this.form.bookName = book.name
      this.form.cover = book.cover
      this.form.score = book.score
    },

    selectUser() {
      const user = this.users.find(v => v.id === this.form.userId)
      this.form.userPhone = user.phone
      this.form.userName = user.name
      this.form.userCardID = user.cardID
    },


    load() {
      request.get('/borrow/page', {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },

    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        bookName: '',
        bookIsbn: '',
        userName: '',

      }
      this.load()
    },

    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },

    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {};
    },

    handleCancel() {
      this.dialogFormVisible = false;
      this.load()
    },


    del(id) {
      request.delete('/borrow/delete/' + id).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },




    save() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.post('/borrow/save', this.form).then(res => {
            if (res.code === '200') {
              this.$message.success('保存成功')
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    },


    update() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.post('/returnBook/save', this.updateForm).then(res => {
            if (res.code === '200') {
              this.$message.success('还书成功')
              this.updateTab = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    },
    returnBook(row){
      this.updateForm = row;
      this.updateTab = true;
    },
    handleCancelUpdate() {
      this.updateTab = false
      this.load()
    },

  }
}
</script>

<style scoped>

</style>